import { getCampusSelector } from '@/api/campus/campus'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import request from '@/utils/request'
import ExportBox from './ExportBox'
import CustomForm from './Form/index.vue'
export default {
  components: { CustomForm, ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        brand: 'bell',
        campusId: undefined,
        type: undefined,
        refundDeposit: undefined,
        refundDate: undefined,
        refundStatus: undefined,
        closureDate: undefined,
        closureNotes: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'campusId', label: '闭校校区' }, { prop: 'type', label: '闭校类别' }, { prop: 'refundDeposit', label: '退还保证金' }, { prop: 'refundAmount', label: '退还金额' }, { prop: 'refundDate', label: '退款时间' }, { prop: 'refundStatus', label: '退款状态' }, { prop: 'refundNotes', label: '退款说明' }, { prop: 'closureDate', label: '闭校时间' }, { prop: 'closureNotes', label: '闭校说明' }, { prop: 'expectedRefundAmount', label: '应退金额' }],
      campusOptions: [],
      typeOptions: [],
      refundDepositOptions: [],
      refundStatusOptions: [],
      brandOptions: []
    }
  },
  computed: {},
  created() {
    this.initData()
    this.gettypeOptions()
    this.getrefundDepositOptions()
    this.getrefundStatusOptions()
    // this.getCampusOptions()
    this.handleChangeBrand('bell')
    this.getBrandOptions()
  },
  methods: {
    handleChangeBrand(val) {
      getCampusSelector().then(res => {
        this.campusOptions = res.data.filter(i => i.brand == val)
      })
    },
    getBrandOptions() {
      getDictionaryDataSelector('Brand').then(res => {
        this.brandOptions = res.data.list
      })
    },
    gettypeOptions() {
      getDictionaryDataSelector('CampusCloseType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    getrefundDepositOptions() {
      getDictionaryDataSelector('CampusCloseDeposit').then(res => {
        this.refundDepositOptions = res.data.list
      })
    },
    getrefundStatusOptions() {
      getDictionaryDataSelector('CampusCloseRefundStatus').then(res => {
        this.refundStatusOptions = res.data.list
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/close`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/system/CampusRecordsClose/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(row, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(row, isDetail)
      })
    },
    handleCommandDown(command) {
      window.open(command)
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `/api/system/CampusRecordsClose/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.location.href = this.define.comUrl + res.data.url
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'closureDate'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'closureDate'
      }
      this.initData()
    }
  }
}
